/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.createUniqueName;
import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getBodies;

import java.util.List;

import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.InteractionConstraint;
import org.eclipse.uml2.uml.OpaqueExpression;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.diagram.sequence.sedi.model.util.OpaqueExpressionUtil;

/**
 * Command to set the body of an interaction constraint. The constraint is given
 * as a String that this command must then parse and convert into a valid UML
 * expression tree.
 * 
 * <p>
 * The following attributes must be set for the command to complete
 * successfully:
 * </p>
 * <ul>
 * <li>Constraint
 * <li>ExpressionString
 * </ul>
 * 
 * <p>
 * <b>Known limitations</b> The selector is always constructed as an
 * OpaqueExpression, and the expression string is always stored in the body #0.
 * </p>
 * 
 * @author frankd@ifi.uio.no
 */
public class SetGuardCommand extends Command {

	private InteractionConstraint constraint;
	private String expressionString, oldExpressionString;
	private boolean createdExpression;
	private boolean addedBody;

	public void setConstraint(final InteractionConstraint constraint) {
		this.constraint = constraint;
	}

	public void setExpressionString(String expressionString) {
		this.expressionString = expressionString;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void execute() {
		if (constraint.getSpecification() == null) {
			oldExpressionString = null;
			OpaqueExpression expr = UMLFactory.eINSTANCE
					.createOpaqueExpression();
			expr.setName(createUniqueName(constraint.getOwnedElements(),
					"Expression_"));
			constraint.setSpecification(expr);
			createdExpression = true;
		}
		if (constraint.getSpecification() instanceof OpaqueExpression) {
			OpaqueExpression expr = (OpaqueExpression) constraint
					.getSpecification();
			List<String> bodies = getBodies(expr);
			if (!bodies.isEmpty()) {
				oldExpressionString = bodies.get(0);
				bodies.set(0, expressionString);
			} else {
				OpaqueExpressionUtil.addBody(expr, expressionString);
				addedBody = true;
			}
		}
		// TODO Handle other expressions, and/or log when there are problems
	}

	@Override
	public void undo() {
		if (constraint.getSpecification() instanceof OpaqueExpression) {
			OpaqueExpression expr = (OpaqueExpression) constraint
					.getSpecification();
			List<String> bodies = getBodies(expr);
			if (addedBody) {
				OpaqueExpressionUtil.removeBody(expr, bodies.size() - 1);
				addedBody = false;
			} else
				bodies.set(0, oldExpressionString);
		}
		if (createdExpression) {
			constraint.setSpecification(null);
			createdExpression = false;
		}
		oldExpressionString = null;
	}

	@Override
	public void redo() {
		this.execute();
	}

	@Override
	public void dispose() {
		expressionString = null;
		oldExpressionString = null;
	}
}
